﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZCSBZPJ.Filter;
using ZCSBZPJ.Models.ZP.Common;
using ZP.Common;
using ZP.DTO.Common;
using ZP.DTO.fz_sys_user;
using ZP.IBLL;

namespace ZCSBZPJ.Controllers
{
    public class FZUserController : BaseController
    {
        //public FZUserController(Ifz_sys_userService _fz_sys_userService)
        //{
        //    this._fz_sys_userService = _fz_sys_userService;
        //}

        [AuthorityFilter]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Edit(int id = 0)
        {
            fz_sys_userResultDTO dto = _fz_sys_userService.GetUser(id);
            return View(dto);
        }

        [AuthorityFilter]
        /// <summary>
        /// 用户信息-模分页模糊查询
        /// </summary>
        /// <returns></returns>
        public JsonResult GetSysUser(fz_sys_userSearchDTO dto)
        {
            PageModel<fz_sys_userResultDTO> pageList = new PageModel<fz_sys_userResultDTO>();
            pageList = _fz_sys_userService.GetSysUser(dto);
            return Json(pageList, JsonRequestBehavior.AllowGet);
        }

        [AuthorityFilter]
        /// <summary>
        /// 保存用户-新增/修改
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="operate">1:重置用户密码</param>
        /// <returns></returns>
        public JsonResult SaveSysUser(fz_sys_userOperateDTO dto, string operate = "")
        {
            ResultData<string> result = new ResultData<string>();
            try
            {
                UserLoginDTO loginUser = (UserLoginDTO)Session[ZPCommon.SESSION_Login];
                dto.updater = loginUser.account;
                result.SubmitResult = _fz_sys_userService.SaveSysUser(dto, operate);
            }
            catch (Exception e)
            {
                result.SubmitResult = false;
                result.Message = e.Message;
            }

            return Json(result);
        }

        [AuthorityFilter]
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <returns></returns>
        public JsonResult DeleteSysUser(List<fz_sys_userOperateDTO> dto)
        {
            ResultData<string> result = new ResultData<string>();
            try
            {
                UserLoginDTO loginUser = (UserLoginDTO)Session[ZPCommon.SESSION_Login];
                result.SubmitResult = _fz_sys_userService.DeleteSysUser(dto, loginUser);
            }
            catch (Exception ex)
            {
                result.SubmitResult = false;
                result.Message = ex.Message;
            }
            return Json(result);
        }

        #region 用户头像修改
        /// <summary>
        /// 上传头像页面
        /// </summary>
        /// <returns></returns>
        [AuthorityFilter]
        public ActionResult Avatar()
        {
            return View();
        }
        /// <summary>
        /// 保存修改的头像
        /// </summary>
        /// <returns></returns>
        [AuthorityFilter]
        public JsonResult SaveAvatar()
        {
            ResultData<string> result = new ResultData<string>();
            try
            {
                UserLoginDTO loginUser = (UserLoginDTO)Session[ZPCommon.SESSION_Login];
                string headImg = Request.Params["headImg"];
                result.SubmitResult = _fz_sys_userService.SaveAvatar(loginUser.id, headImg);
                if (result.SubmitResult)
                {
                    loginUser.headImg = headImg;
                    Session[ZPCommon.SESSION_Login] = loginUser;
                }
            }
            catch (Exception ex)
            {
                result.SubmitResult = false;
                result.Message = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 上传商品图片(图片转为base64编码的文本)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadFile()
        {
            FileResultData fileResult = new FileResultData { SaveMode = FileSaveModeEnum.Base64 };
            var oFile = Request.Files[0];
            string errorMsg = "";
            if (oFile == null)
            {
                errorMsg = "没有找到文件！ErrorMsg: not found Request.Files.";

            }
            else
            {
                fileResult.Content = FileHelper.GetFileBase64ByImg(oFile, out errorMsg);
            }
            if (!string.IsNullOrWhiteSpace(errorMsg))
            {
                fileResult.Message = errorMsg;
            }
            else
            {
                fileResult.Extension = System.IO.Path.GetExtension(oFile.FileName); //得到扩展名
                fileResult.FileName = oFile.FileName;
                fileResult.SubmitResult = true;
            }
            #region 物理存储方式
            //List<string> filePathList = new List<string>();
            //List<string> fileNameList = new List<string> ();
            //string filePath = Server.MapPath("~/File/psi/upload/goods/");
            //foreach (var file in Request.Files)
            //{
            //    fileNameList.Add(oFile.FileName);
            //    filePathList.Add(filePath);
            //    oFile.SaveAs(Path.Combine(filePath, oFile.FileName));
            //}
            #endregion
            return Json(fileResult);
        }
        #endregion

        #region 修改密码
        public ActionResult UpdatePassword()
        {
            return View();
        }

        #endregion

        #region 忘记密码
        /// <summary>
        /// 忘记密码页面
        /// </summary>
        /// <returns></returns>
        public ActionResult FindPassword()
        {
            return View();
        }
        /// <summary>
        /// 设置密码页面
        /// </summary>
        /// <returns></returns>
        public ActionResult SetPassword()
        {
            string sid = Request.Params["sid"];

            //sid无效则跳转到登录
            if (string.IsNullOrWhiteSpace(sid))
            {
                return RedirectToAction("index", "Login");
            }

            //sid解码
            string errorMsg = "";
            string account = "";
            bool result = _fz_sys_userService.FindPasswordDecrypt(sid, out account, out errorMsg);
            if (!result)
            {
                if (errorMsg == "")
                    return RedirectToAction("index", "Login");  //没有错误信息，直接跳转到登录
                else
                {
                    ViewBag.errorMsg = errorMsg; //保留错误信息放到页面显示
                    return View();
                }
            }

            ViewBag.account = account;
            //查询email对应账号
            /*TB_Sys_UserResultDTO user = _TB_Sys_UserServices.GetOne(account);
            ViewBag.account = user.account;*/
            return View();
        }

        /// <summary>
        /// 忘记密码-发送验证链接到邮箱
        /// </summary>
        /// <returns></returns>
        public JsonResult SendVerification()
        {
            ResultData<string> result = new ResultData<string>();
            try
            {
                string account = Request.Params["account"];
                result.SubmitResult = _fz_sys_userService.SendVerification(account);
            }
            catch (Exception e)
            {
                result.SubmitResult = false;
                result.Message = e.Message;
            }

            return Json(result);
        }

        /// <summary>
        /// 保存新密码
        /// </summary>
        /// <returns></returns>
        public JsonResult SavePassword()
        {
            ResultData<string> result = new ResultData<string>();
            try
            {
                string account = Request.Params["account"];
                string password = Request.Params["password"];
                result.SubmitResult = _fz_sys_userService.SavePassword(account, password);
            }
            catch (Exception e)
            {
                result.SubmitResult = false;
                result.Message = e.Message;
            }
            return Json(result);
        }

        #endregion
    }
}